// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports


namespace SCIEF.SysBFW.CommonExt
{
	namespace Utils
	{
		public sealed class FileExt
		{
			/// <summary>
			/// Take a fileName an return a scramble string.
			/// </summary>
			/// <param name="fileName"></param>
			/// <returns></returns>
			/// <remarks></remarks>
			public static string EncodedFileName(string fileName, string fileExtention = "stbx")
			{
				string encodedString = default(string);
				CryptographyHelper cryptoHelper = new CryptographyHelper(CryptographyHelper.EncodingBaseTypes.Hex);
				encodedString = cryptoHelper.GetHash(fileName, CryptographyHelper.HashTypes.MD5);
				return string.Format("{0}.{1}", encodedString, fileExtention);
			}
			
			/// <summary>
			///
			/// </summary>
			/// <param name="fileName"></param>
			/// <param name="path"></param>
			/// <returns></returns>
			/// <remarks></remarks>
			public static bool EncryptTextFileContent(string fileName, string path = "")
			{
				bool returnValue = default(bool);
				CryptographyHelper cryptoHelper = new CryptographyHelper(CryptographyHelper.EncodingBaseTypes.Base64);
				string fullFileName = System.IO.Path.Combine(path, fileName);
				returnValue = false;
				try
				{
					if (System.IO.File.Exists(fullFileName))
					{
						//TestPoint : This is the KEY for encryption
						string encryptedText = cryptoHelper.Encrypt(System.IO.File.ReadAllText(fullFileName), "QaZPlM63!");
						System.IO.File.WriteAllText(fullFileName, encryptedText);
					}
					else
					{
						throw (new FileNotFoundException("File Not found in EncryptTextFileContent", fileName));
					}
					returnValue = true;
				}
				catch (Exception ex)
				{
					throw (ex);
				}
				return returnValue;
			}
			
			/// <summary>
			///
			/// </summary>
			/// <param name="fileName"></param>
			/// <param name="path"></param>
			/// <returns></returns>
			/// <remarks></remarks>
			public static string DecryptTextFileContent(string fileName, string path = "")
			{
				string returnValue = default(string);
				CryptographyHelper cryptoHelper = new CryptographyHelper(CryptographyHelper.EncodingBaseTypes.Base64);
				string fullFileName = System.IO.Path.Combine(path, fileName);
				returnValue = null;
				try
				{
					if (System.IO.File.Exists(fullFileName))
					{
						//TestPoint : This is the KEY for encryption
						returnValue = cryptoHelper.Decrypt(System.IO.File.ReadAllText(fullFileName), "QaZPlM63!");
					}
					else
					{
						throw (new FileNotFoundException("File Not found in DecryptTextFileContent", fileName));
					}
				}
				catch (Exception ex)
				{
					throw (ex);
				}
				return returnValue;
			}
		}
	}
	
	
}
